package com.zhkj.txg.module.find.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.zhkj.lib.base.ViewModelFragment
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.find.adapter.FindAdapter
import com.zhkj.txg.module.find.vm.FindViewModel
import kotlinx.android.synthetic.main.fragment_find.*

class FindFragment : ViewModelFragment<FindViewModel>() {
    override fun setViewModel(): FindViewModel = createViewModel(false)
    override val layoutResId: Int = R.layout.fragment_find

    private val requestCodeRefresh = 10000
    private val findAdapter = FindAdapter().apply {
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.findList(AccountManager.getToken())
        }
    }
    private var refresh = false

    override fun initUI() {
        titleFind.setFragmentStatusBar()
        with(rvFind) {
            layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
//            layoutManager = GridLayoutManager(context, 2)
            adapter = findAdapter
            findAdapter.setEmptyView(R.layout.layout_empty_data)
        }
        ivFindPost.setOnClickListener {
            FindArticlePublishActivity.publishArticle(this@FindFragment, requestCodeRefresh)
        }
    }

    override fun initData() {
        viewModel.findList(AccountManager.getToken()).takeIf {
            !it.hasObservers()
        }?.observe(viewLifecycleOwner, Observer { it ->
            when (it.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> it.response?.data?.let {
                    ivFindPost.visibility = if (it.isMember) View.VISIBLE else View.GONE
                    // 数据填充
                    if (it.isLoadMore) findAdapter.addData(it.list)
                    else findAdapter.setNewData(it.list)
                    // 判断加载状态
                    if (it.hasMore) findAdapter.loadMoreModule?.loadMoreComplete()
                    else findAdapter.loadMoreModule?.loadMoreEnd()
                }
                HttpResponseStatus.Status.FAILURE -> {
                    it.exception?.printStackTrace()
                    toast(it.statusTip)
                    findAdapter.loadMoreModule?.loadMoreFail()
                }
            }
        })
    }

    override fun initViewModel() {
        // 账号变更观察者
        viewModel.accountChangeSubscribe().observe(viewLifecycleOwner, Observer { refresh = true })
        // 会员状态变更观察者
        viewModel.memberStatusChangeSubscribe().observe(viewLifecycleOwner, Observer { refresh = true })
        // 退出登录观察者
        viewModel.logoutSubscribe().observe(viewLifecycleOwner, Observer { refresh = true })
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden && refresh) {
            initData()
            refresh = false
        }
    }

    override fun onResume() {
        super.onResume()
        if (refresh) {
            initData()
            refresh = false
        }
    }
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (resultCode == Activity.RESULT_OK && requestCode == requestCodeRefresh) {
//            viewModel.findList(AccountManager.getToken(), true)
//        }
//    }
}